#include <iostream>
#include <string>

#include "List.h"

using namespace std;

int main(int argc, char const *argv[])
{
    List list;
    list.loadData();

    // 如果没有用户，则提示创建第一个用户
    if (list.userList.empty())
    {
        string username, password;
        cout << "系统中暂无用户，请创建第一个用户：" << endl;
        cout << "请输入用户名：";
        cin >> username;
        cout << "请输入密码：";
        cin >> password;
        list.addUser(username, password);
        cout << "用户创建成功！" << endl;
        list.saveData();
    }

    cout << "欢迎来到学生管理系统" << endl;
    string username;
    string password;
    cout << "请输入用户名：";
    cin >> username;
    cout << "请输入密码：";
    cin >> password;
    if (list.login(username, password))
    {
        cout << "登录成功" << endl;
        int choice;
        while (true)
        {
            cout << "请选择操作：1.添加学生 2.删除学生 3.修改学生 4.查询学生 5.添加用户 6.删除用户 7.退出" << endl;
            cin >> choice;
            string name;
            int id;
            double score;
            switch (choice)
            {
            case 1:
                cout << "请输入学生姓名：";
                cin >> name;
                cout << "请输入学生学号：";
                cin >> id;
                cout << "请输入学生成绩：";
                cin >> score;
                list.addStudent(name, id, score);
                list.saveData();
                break;
            case 2:
                cout << "请输入要删除的学生学号：";
                cin >> id;
                list.deleteStudent(id);
                list.saveData();
                break;
            case 3:
                cout << "请输入要修改的学生学号：";
                cin >> id;
                cout << "请输入要修改的学生成绩：";
                cin >> score;
                list.updateStudentScore(id, score);
                list.saveData();
                break;
            case 4:
                int id;
                cout << "请输入要查询的学生学号：";
                cin >> id;
                list.checkStudentScore(id);
                list.saveData();
                break;
            case 5:
                cout << "请输入要添加的用户名：";
                cin >> username;
                cout << "请输入要添加的密码：";
                cin >> password;
                list.addUser(username, password);
                list.saveData();
                break;
            case 6:
                cout << "请输入要删除的用户名：";
                cin >> username;
                list.deleteUser(username);
                list.saveData();
                break;
            case 7:
                cout << "退出系统" << endl;
                return 0;
            default:
                cout << "输入错误" << endl;
                break;
            }
        }
    }
    else
    {
        cout << "用户名或密码错误" << endl;
    }
    return 0;
}
